<?php
class ProductCategorys extends Eloquent{

	protected $guarded  = array("id");

    use SoftDeletingTrait;

    protected $dates = ['deleted_at'];
	
	//protected $fillable = array('name', 'parent_id', 'color');
	
	public function farther(){
		//'local_key', 'parent_key');
		return $this->belongsTo('ProductCategorys', 'parent_id');
	}
	
	public function children(){
		//'foreign_key', 'local_key'
		return $this->hasMany("ProductCategorys","parent_id");
	}
	
	public function products(){
		return $this->hasMany("Products","category_id");
	}
	
	public function scopeRoot($query){
        return $query->where('parent_id', '=', 0);
    }
    
    public static function tree(){
    	return  ProductCategorys::with(array('children' => function($query){
    		$query->orderBy('order', 'desc');
    	}))->orderBy('order','desc')->where('parent_id',0)->get();
    }

    public static function treeAll(){
        return  ProductCategorys::with(array('children' => function($query){
            $query->withTrashed()->orderBy('order', 'desc');
        }))->withTrashed()->orderBy('order','desc')->where('parent_id',0)->get();
    }

}